import router from "@/router";
import nprogress from "nprogress"
import "nprogress/nprogress.css"
import store from "@/store"

//白名单
const whiteList = ["/login", "/404"]
    /**
     * 前置守卫
     */

router.beforeEach(async(to, from, next) => {
    nprogress.start()
    if (store.getters.token) {
        //存在token
        //跳转主页
        if (to.path === "/login") {
            next("/") //中转页面,没有执行后置守卫
            nprogress.done()
        } else {
            //判断是否获取过资料
            // if (!store.getters.userId) {
            //     await store.dispatch('user/getUserInfo')
            // }
            next()
        }
    } else {
        //不存在token
        if (whiteList.includes(to.path)) { //是否在白名单
            next()
        } else {
            next("/login")
            nprogress.done()
        }
    }
})

/**
 * 后置守卫
 */
router.afterEach(() => {
    nprogress.done()
})